package org.jeecg.modules.pacs.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
//import com.google.gson.Gson;
import org.jeecg.modules.pacs.entity.*;
import org.jeecg.modules.pacs.mapperHis.FindHisHealthCardDao;
import org.jeecg.modules.pacs.mapperPacs.FindPacsReportDao;
import org.jeecg.modules.pacs.service.FindHisHealthCardService;
import org.jeecg.modules.pacs.service.FindPacsReportService;
import org.jeecg.modules.pacs.handle.Request;
import org.jeecg.modules.pacs.handle.RequestUtil;
import org.jeecg.modules.pacs.handle.ResultEnum;
import org.jeecg.modules.pacs.uitl.HttpPostCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

@Service
@DS("pacs")
public class FindPacsReportServiceImpl implements FindPacsReportService {
    public static final String HOSPITAL_HIS = "pacsCode.properties";
    public static final String HOSPITAL_APP = "hospitalApp.properties";
    @Autowired
    FindPacsReportDao findPacsRepotrDao;

    @Autowired
    FindHisHealthCardService findHisHealthCardService;
    RequestUtil requestUtil = new RequestUtil();

    public Request<List<ReportTable>> findPacsReportByIdcadrandTime(HealthCard healthCard) {
        if("01".equals(healthCard.getType())){
            if ( "01".equals(healthCard.getIdType())&&healthCard.getIdCardNo() != null && !"".equals(healthCard.getIdCardNo())) {
                List<ReportTable> list = findPacsRepotrDao.findRepotrTableByIdcardAndTime(healthCard);

                return iteratorList(list);
            } else if( "11".equals(healthCard.getCardType())&&healthCard.getCardNo() != null && !"".equals(healthCard.getCardNo())){
                List<ReportTable> list = findPacsRepotrDao.findRepotrTableByHealthCardAndTime(healthCard);
               return iteratorList(list);
            }
            else {
                return requestUtil.lose(ResultEnum.UNKONW_ERROR);
            }
        }else{
            return requestUtil.lose(ResultEnum.UNKONW_ERROR);
        }

    }

    /**
     * 遍历修改查询内容
     * @param list
     * @return
     */
    private Request<List<ReportTable>> iteratorList(List<ReportTable> list){
        if (list == null || list.size() == 0) {
            return requestUtil.lose(ResultEnum.UNKONW_ERROR);
        }
        Iterator<ReportTable> iterator =list.iterator();
        while(iterator.hasNext()){
            upRep(iterator.next());
        }
        return requestUtil.success(ResultEnum.SUCCESS, list);
    }

    /**
     * 修改ReportTable实体类
     * @param reportTable
     */
    private void upRep(ReportTable reportTable){
        reportTable.setResult(reportTable.getResult().replace("\n",",").replace("\t","").replace("\\",""));
        reportTable.setWords(reportTable.getWords().replace("\n",",").replace("\t","").replace("\\",""));
    }
    @Override
    public Request<ReportTable> findRepotrTableByReportIdAndType(ReportId reportId) {
        if (reportId.getReportId() != null && !"".equals(reportId.getReportId())) {
            ReportTable reportTable = findPacsRepotrDao.findRepotrTableByReportIdAndType(reportId);
            if (reportTable == null) {
                return requestUtil.lose(ResultEnum.UNKONW_ERROR);
            }
            upRep(reportTable);
            return requestUtil.success(ResultEnum.SUCCESS, reportTable);
        } else {
            return requestUtil.lose(ResultEnum.UNKONW_ERROR);
        }
    }

    @Override
    public List<ReportTable> findRepotrTableByTime() {
       //HealthCard time =  new HealthCard();
       HealthCard time = findPacsRepotrDao.findTime();
       // HealthCard time = findPacsRepotrDao.sssfindTimess();
         //time.setStartTime("2019-11-13 10:58:28");
        // time.setEndTime("2019-11-13 11:00:28");
        System.out.println(time);
        List<ReportTable> list = findPacsRepotrDao.findRepotrTableByTime(time);
        //ReportTable r = new ReportTable();
//        if (list != null && list.size() != 0) {
//            for (ReportTable r : list) {
//                try {
//                    System.out.println(r.toString());
//                    //电子健康卡号
//                    String healthCard;
//                    if (r.getCheckNo() == null) {
//                        String hospital = readProFile(r.getType(),HOSPITAL_HIS);
//                        System.out.println(hospital);
//                        healthCard = findHisHealthCardService.findHealthCard(hospital, r.getCardNo());
//                        if (healthCard == null || "".equals(healthCard)) {
//                            continue;
//                        }
//                    } else {
//                        healthCard = r.getCheckNo();
//                    }
//                   // healthCard="17Y9400001528";
//                    String url = "http://11.103.9.9/ehc-portal-push/sms/send";
//                    InputParam inputParam = new InputParam();
//                    Param param = new Param();
//                    Content content = new Content();
//                    ExtraParams extraParams = new ExtraParams();
//                    String appId = readProFile(r.getType(),HOSPITAL_APP);
//                    inputParam.setAppId(appId);
//                    inputParam.setTransType("sms.send.checkresult");
//                    content.setFirst("sms.send.checkresult.first");
//                    content.setKeyword1(r.getPatientName());
//                    content.setKeyword2(healthCard);
//                    if (content.getKeyword2() == null) {
//                        content.setKeyword2("");
//                    }
//                    if (content.getKeyword3() == null) {
//                        content.setKeyword3("");
//                    }
//                    content.setKeyword3(r.getCheckType());
//                    content.setKeyword4(r.getDepartName());
//                    String remark =r.getResult().replace("\n","").replace("\t","").replace("\\","");
//                    content.setRemark(remark);
//                    param.setContent(content);
//                    param.setCardNo(healthCard);
//                    param.setCardType("11");
//                    param.setReqNo("");
//                    param.setRedirectUrl("");
//                    extraParams.setReportCategoryCode("1");
//                    extraParams.setReportType("1");
//                    extraParams.setReportNo(r.getReportNo());
//                    param.setExtraParams(extraParams);
//                    inputParam.setParam(param);
//                    Gson gs = new Gson();
//                    String jsonStr = gs.toJson(inputParam);
//                    System.out.println(jsonStr);
//                    // String param = "{  \"appId\":\"1DNUJKJV00000100007F0000F0308269\", \n" + "\t\"transType\":\"sms.send.waitpay\",\n" + "    \"param\":{\n" + "    \t\"content\": {\n" + "        \t\"first\": \"sms.send.waitpay.first\",\n" + "            \"keyword1\": \"张伟\",\n" + "            \"keyword2\": \"儿科\",\n" + "            \"keyword3\": \"门诊结算\",\n" + "            \"keyword4\": \"李哲\",\n" + "            \"keyword5\": \"2018-08-15 09:22:49 \",\n" + "            \"remark\": \"sms.send.waitpay.remark\",\n" + "            \"extra1\":\"药品费：4.00 元\",\n" + "            \"extra2\":\"检查费：60.00 元\",\n" + "            \"extra3\":\"检验费：7.00 元\", \n" + "            \"extra4\":\"其他费：90.00 元\" },\n" + "        \"redirectUrl\": \"http://www.baidu.com\",\n" + "        \"cardNo\":\"372559EAB155114117F0B313F5F389174D6D9267385933511474B27D21038B27\",\n" + "        \"cardType\":\"11\",\n" + "        \"reqNo\":\"7009140\" ,\n" + "        \"extraParams\":{\n" + "        \t\"reportNo\":\"13140485\",\n" + "        \t\"reportType\":\"2\",\n" + "            \"reportCategoryCode\":\"2\"\n" + "         } \n" + "      } \n" + " } ";
//                    String result = HttpPostCheck.sendPost(url, jsonStr);
//                    System.out.println(result);
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        }
        return list;
    }

    /**
     * 根据医院编码读取properties文件中对应内容
     * @param code
     * @param proFileName
     * @return
     */
    private  static  String readProFile(String code,String proFileName){
        Properties prop = new Properties();
        try {
            prop.load(new InputStreamReader(FindPacsReportServiceImpl.class.getClassLoader().getResourceAsStream(proFileName), "utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String hospital = prop.getProperty(code);
        return hospital;
    }
}
